// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    // 🔐 认证相关
    {
      path: '/auth',
      component: () => import('@/layouts/AuthLayout.vue'),
      children: [
        {
          path: 'login',
          name: 'Login',
          component: () => import('@/pages/auth/Login.vue'),
          meta: { title: '用户登录', requiresAuth: false }
        }
      ]
    },

    // 🏠 主应用
    {
      path: '/',
      component: () => import('@/layouts/index.vue'),
      redirect: '/dashboard', // 进站默认去工作台
      meta: { requiresAuth: true },
      children: [
        // 📊 仪表盘
        {
          path: 'dashboard',
          name: 'Dashboard',
          component: () => import('@/pages/dashboard/Index.vue'),
          meta: {
            title: '工作台',
            icon: 'Dashboard',
            permission: 'dashboard:read',
            affix: true,              // 固定在 TagsView
            keepAlive: false
          }
        },

        // ✍️ 文章（示例按你提供的片段还原并清理）
        {
          path: 'content/archives',
          component: () => import('@/layouts/SimpleLayout.vue'),
          meta: { title: '文章管理', icon: 'Document' },
          children: [
            {
              path: 'list',
              name: 'ArchivesList',
              component: () => import('@/pages/archives/Index.vue'),
              meta: { title: '文章列表', permission: 'archives:read', keepAlive: true }
            },
            {
              path: 'create',
              name: 'ArchivesCreate',
              component: () => import('@/pages/archives/Create.vue'),
              meta: {
                title: '发布文章',
                permission: 'archives:create',
                activeMenu: '/content/archives/list'
              }
            },
            {
              path: 'edit/:id',
              name: 'ArchivesEdit',
              component: () => import('@/pages/archives/Edit.vue'),
              meta: {
                title: '编辑文章',
                permission: 'archives:update',
                activeMenu: '/content/archives/list'
              }
            }
          ]
        },

        // 📁 文件
        {
          path: 'files',
          name: 'Files',
          component: () => import('@/pages/files/Index.vue'),
          meta: {
            title: '附件管理',
            icon: 'Paperclip',
            permission: 'attachment:read',
            keepAlive: true
          }
        },

        // ⚙️ 系统（示例清理重复）
        {
          path: 'system',
          component: () => import('@/layouts/SimpleLayout.vue'),
          meta: { title: '系统管理', icon: 'Setting' },
          children: [
            {
              path: 'roles',
              name: 'SystemRoles',
              component: () => import('@/pages/system/Roles.vue'),
              meta: { title: '角色管理', icon: 'UserFilled', permission: 'role:read' }
            },
            {
              path: 'permissions',
              name: 'SystemPermissions',
              component: () => import('@/pages/system/Permissions.vue'),
              meta: { title: '权限管理', icon: 'Key', permission: 'permission:read' }
            },
            {
              path: 'menus',
              name: 'SystemMenus',
              component: () => import('@/pages/system/Menus.vue'),
              meta: { title: '菜单管理', icon: 'Menu', permission: 'menu:read' }
            }
          ]
        },

        // 🧾 日志 / 设置
        {
          path: 'logs',
          name: 'SystemLogs',
          component: () => import('@/pages/logs/Index.vue'),
          meta: { title: '系统日志', icon: 'Document', permission: 'logger:read', keepAlive: true }
        },
        {
          path: 'settings',
          name: 'SystemSettings',
          component: () => import('@/pages/settings/Index.vue'),
          meta: { title: '系统设置', icon: 'Tools', permission: 'setting:read' }
        }
      ]
    },

    // 🧪 测试
    {
      path: '/test',
      name: 'Test',
      component: () => import('@/pages/Test.vue'),
      meta: { title: '测试页面', requiresAuth: false }
    },

    // 🚫 错误页
    { path: '/403', name: 'Forbidden', component: () => import('@/pages/error/403.vue'), meta: { title: '无权限访问', requiresAuth: false } },
    { path: '/404', name: 'NotFound', component: () => import('@/pages/error/404.vue'), meta: { title: '页面不存在', requiresAuth: false } },
    { path: '/500', name: 'ServerError', component: () => import('@/pages/error/500.vue'), meta: { title: '服务器错误', requiresAuth: false } },

    // 通配
    { path: '/:pathMatch(.*)*', redirect: '/404' }
  ]
})

export default router